from typing import Optional, List

from fastapi import APIRouter, Depends, Query, Body
from sqlalchemy.orm import Session

from server.app.cloud_ide.model import IdeRegistry
from server.app.cloud_ide.schema.ide_registry.request.ide_registry_create_request import IdeRegistryCreateRequest
from server.app.cloud_ide.schema.ide_registry.request.ide_registry_update_request import IdeRegistryUpdateRequest
from server.app.cloud_ide.service.ide_registry_service import IdeRegistryService
from server.framework.core.http_schemas import CommonResponseSchema, TableResponseSchema
from server.framework.core.injects import get_db
from server.framework.core.schemas import OptionsSchema

ide_registry_api = APIRouter()
ide_registry_service = IdeRegistryService()


@ide_registry_api.get("/get_registry_options", name='获取镜像源下拉选项', response_model=List[OptionsSchema])
async def get_registry_options(db: Session = Depends(get_db)) -> List[OptionsSchema]:
    results = ide_registry_service.get_all_ide_registry(db)

    option_results = []
    for row in results:
        option_results.append(OptionsSchema(value=str(row.id), label=row.name))
    return option_results


@ide_registry_api.get("/get_entity", name='获取单个镜像源')
async def get_entity(id: str = Query(None, description='镜像源id'),
                     db: Session = Depends(get_db)) -> IdeRegistry:
    entity = ide_registry_service.get_ide_registry_by_id(db, id)
    return entity


@ide_registry_api.get("/list", response_model=TableResponseSchema, name='镜像源列表')
async def list(
        name: str = Query(None, description='过滤条件:镜像源名称'),
        enable: int = Query(None, description='过滤条件:镜像源是否启用(0:禁用,1:启用)'),
        current: Optional[int] = Query(1, description='当前页数'),
        page_size: Optional[int] = Query(10, description='每页记录的数量', alias='pageSize'),
        db: Session = Depends(get_db)) -> TableResponseSchema:
    """
    获取镜像源列表
    :return:
    """
    items, total = ide_registry_service.get_ide_registry_list(db, name=name,
                                                              enable=enable, page_size=page_size,
                                                              current_page=current)
    return TableResponseSchema(data=items, total=total, page=page_size, success=True)


@ide_registry_api.post("/create", response_model=CommonResponseSchema, name='创建镜像源')
async def create(data: IdeRegistryCreateRequest = Body(None, description='镜像源创建请求'),
                 db: Session = Depends(get_db)) -> CommonResponseSchema:
    """
    创建镜像源
    :return:
    """
    rs = ide_registry_service.create_ide_registry(db, data)
    if rs:
        return CommonResponseSchema(data='', message=f'创建成功', success=True)
    else:
        return CommonResponseSchema(data='', message=f'创建失败', success=False)


@ide_registry_api.get("/delete", response_model=CommonResponseSchema, name='删除镜像源')
async def delete(
        ids: str = Query(None, description='镜像源id，多条记录以逗号分割'),
        db: Session = Depends(get_db)) -> CommonResponseSchema:
    delete_ids = ids.split(',')
    rs = ide_registry_service.delete_ide_registry(db, delete_ids)
    if rs:
        return CommonResponseSchema(data={}, message=f'删除成功', success=True)
    else:
        return CommonResponseSchema(data={}, message=f'删除失败', success=False)


@ide_registry_api.post("/edit", response_model=CommonResponseSchema, name='编辑镜像源')
async def edit(data: IdeRegistryUpdateRequest = Body(None, description='镜像源编辑实体'),
               db: Session = Depends(get_db)) -> CommonResponseSchema:
    rs = ide_registry_service.update_ide_registry(db, data)
    if rs:
        return CommonResponseSchema(data={}, message=f'编辑成功', success=True)
    else:
        return CommonResponseSchema(data={}, message=f'编辑失败', success=False)
